package com.jialea.springboot.mq;

import com.alibaba.fastjson2.JSONObject;
import com.jialea.springboot.model.dto.mq.EmailDto;
import com.jialea.springboot.service.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.jialea.springboot.common.constants.EmailConstants.*;

/**
 * @author: wf
 * @created: 2024/6/23 12:04
 */

@Slf4j
@Component
public class EmailConsumerListener {

    @Autowired
    private EmailService emailService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = TEXT_QUEUE),
            exchange = @Exchange(name = EMAIL_EXCHANGE, type = ExchangeTypes.DIRECT),
            key = TEXT_KEY
    ))
    public void textListeningQueue(String msg) {
        log.info("textListeningQueue监听到消息：{}",msg);
        EmailDto emailDto = JSONObject.parseObject(msg, EmailDto.class);
        emailService.sendSimpleMail(emailDto.getTo(),emailDto.getSubject(),emailDto.getContent());
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = HTML_QUEUE),
            exchange = @Exchange(name = EMAIL_EXCHANGE, type = ExchangeTypes.DIRECT),
            key = HTML_KEY
    ))
    public void htmlListeningQueue(String msg) {
        System.out.println("消费者接收到queue3的消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = ATTACHMENTS_QUEUE),
            exchange = @Exchange(name = EMAIL_EXCHANGE, type = ExchangeTypes.DIRECT),
            key = ATTACHMENTS_KEY
    ))
    public void attachmentsListeningQueue(String msg) {
        System.out.println("消费者接收到queue3的消息：【" + msg + "】");
    }
}
